package com.javastudy.autowiredStudy.restTemplateStudy;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Repository;

import java.io.IOException;

/**
 * 在使用restTemplate调用远程接口，有时候需要传递traceID，便于查询日志的时候能够串联一次完整的请求链路，快速定位问题
 * @author mac
 * @date 2021/9/7 5:57 下午
 */
@Repository
public class RestTemplateIntercepter implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().set("traceID",MdcUtils.get());
        return execution.execute(request,body);
    }
}
